package com.liushao.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.liushao.config.ContantsProperties;
import com.liushao.service.MsmService;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
/**
 * @program: springcloudalibabafinance
 * @ClassName MsmServiceImpl
 * @description:
 * @author: 陈修华
 * @create: 2025−01-08 15:15
 * @Version 1.0
 **/
@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public boolean send(String phone, String code) {
        if(StringUtils.isEmpty(phone)){
            return false;
        }

        //设置发送短信的参数
        DefaultProfile profile = DefaultProfile.
                getProfile(ContantsProperties.REGION_ID, // 区域id
                        ContantsProperties.ACCESS_KEY_ID, // key
                        ContantsProperties.ACCESSKEY_SSECRET); // 密码

        //发送短信的客户端
        IAcsClient client = new DefaultAcsClient(profile);
        //设置客户端参数
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);//发送方式
        request.setDomain("dysmsapi.aliyuncs.com"); // 阿里云的域
        /*这里是*/
        request.setVersion("2017-05-25"); // TODO模版号要匹配
        request.setAction("SendSms");
        //设置发送手机号
        request.putQueryParameter("PhoneNumbers",phone); //TODO前面是固定的
        //签名 TODO
        request.putQueryParameter("SignName","启航");
        //设置发送短信儿模板 TODO
        request.putQueryParameter("TemplateCode","SMS_478135011"); // TODO去我们设置的地方复制


        Map<String,Object> param = new HashMap<>();
        param.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));


        try {
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();

            return success;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean send(String phone, String code, Map<String, Object> paramMap) {
        if(StringUtils.isEmpty(phone)){
            return false;
        }

        //设置发送短信的参数
        DefaultProfile profile = DefaultProfile.
                getProfile(ContantsProperties.REGION_ID,
                        ContantsProperties.ACCESS_KEY_ID,
                        ContantsProperties.ACCESSKEY_SSECRET);

        //发送短信的客户端
        IAcsClient client = new DefaultAcsClient(profile);
        //设置客户端参数
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);//发送方式
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        //设置发送手机号
        request.putQueryParameter("PhoneNumbers",phone); //TODO前面是固定的
        //签名 TODO
        request.putQueryParameter("SignName","启航");
        //设置发送短信儿模板 TODO
        request.putQueryParameter("TemplateCode","SMS_478135011"); // TODO去我们设置的地方复制



/*        Map<String,Object> param = new HashMap<>();
        param.put("code",code);*/
        // 和Json有什么区别
        Gson gson = new Gson();

        String jsonParam = gson.toJson(paramMap);

        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(jsonParam));


        try {
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();

            return success;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }
}
